package com.ggs.starbug.stream;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author lianghaohui
 * @Date 2022/1/7 9:39
 * @Description
 */
public class MapAndFlatMap {

    private static List<String> LIST = Arrays.asList("https://", "www", ".", "javastack", ".", "cn");

    @Test
    public void test1() {
        List<String> mapList = LIST.stream().map(e -> e.concat("---")).collect(Collectors.toList());
        mapList.forEach(System.out::print);
        System.out.println(mapList.size());
    }

    private static List<String> NUMBERS_LIST = Arrays.asList("22", "19", "89", "90");

    @Test
    public void test2() {
        System.out.println("=====map to long list=====");
        List<Long> longList = NUMBERS_LIST.stream().mapToLong(Long::valueOf).boxed().collect(Collectors.toList());
        longList.forEach(System.out::println);
        System.out.println("map to long list size: " + longList.size());
        System.out.println();
    }


    String[] arr1 = {"https://", "www", ".", "javastack", ".", "cn"};
    String[] arr2 = {"公众号", ":", "Java技术栈"};
    String[] arr3 = {"作者", ":", "栈长"};

    @Test
    public void test3() {
        List<String[]> list = Stream.of(arr1, arr2, arr3).collect(Collectors.toList());
        list.forEach(System.out::print);
        //打印了三个数组元素
        System.out.println(list.size());

        // map 适用于对每个元素进行简单的转换，flatMap 适用于对数组流进行平铺后合并，两个方法的应用场景不一样。
        List<String> flatMapList = list.stream().flatMap(arr -> Arrays.stream(arr)).collect(Collectors.toList());
        // 扁平化后，打印数组的内容
        flatMapList.forEach(System.out::print);
    }

}
